package com.codecentric.sample.store.service;

import org.springframework.stereotype.Repository;

@Repository
public class PrimeJudger {
    static final int N=1010;
    static private int[] primes=new int[N];
    static private boolean[] st=new boolean[N];
    static private int cnt=0;

    public void init(){
        //线性筛
        int n=1000;
        for (int i = 2; i <= n; i ++ ) {
            if (!st[i]) primes[cnt ++ ] = i;
            for (int j = 0; primes[j] <= n / i; j ++ ) {
                st[primes[j] * i] = true;
                if (i % primes[j] == 0) break;

            }
        }
    }

    public String judgeNumber(int n){
        if(n<=0||n>1000) return "超出范围";
        if(n==1) return "不是质数";
        if(!st[n]) return "是质数";
        else return "不是质数";
    }

}
